package cn.hhchat.wp.controller;

import cn.hhchat.wp.dto.msg.RMsg;
import cn.hhchat.wp.dto.msg.RMsgFactory;
import cn.hhchat.wp.entity.AttachmentEntity;
import cn.hhchat.wp.exception.InValidFieldsException;
import cn.hhchat.wp.service.AttachmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * Created this one by HMH on 2017/10/24.
 */
@RestController
public class AttachmentController {

    @Value("${sei.resource.urlRoot}")
    private String urlRoot;

    @PostMapping("/upload")
    public RMsg upload(@RequestParam("file") MultipartFile file) {
        if(file.isEmpty()){
            throw new InValidFieldsException("文件为空");
        }
        AttachmentEntity attachment = attachmentService.save(file);
        return RMsgFactory.ok(attachment.toSimpleAttachment(urlRoot));

    }



    @Autowired
    AttachmentService attachmentService;

}
